﻿//0-360度组件
//用于调节360度对象使用
//version 1.0 
//author 夏天的树人
package org.summerTree.components
{
     
	import flash.display.Sprite;
	import flash.events.*;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.filters.*;
	import flash.geom.*;
	import flash.filters.BevelFilter;
	import flash.text.*;
	
	 
	public class CircleSlider extends Sprite
	{
		    
		private var values:Number=0;
		private var minValue:Number=0;
		private var maxValue:Number=100;
		private var bar_control:Sprite;
        private var msg:TextField=new TextField();
		public function CircleSlider(minValue:Number=0,maxValue:Number=360)
		{			
			this.minValue=minValue;
			this.maxValue=maxValue;
			addEventListener(Event.ADDED_TO_STAGE,init);
		}

		private function init(event:Event=null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,init);
			var bottom:Shape=new Shape();
			addChild(bottom);
			bottom.x=0;
			bottom.y=0;
			bottom.graphics.lineStyle();
			bottom.graphics.beginFill(0xFFFFFF);
			bottom.graphics.drawCircle(0,0,60);
			bottom.graphics.endFill();
		
            //发光滤镜
			var glow:GlowFilter=new GlowFilter();
			glow.color=0xcccccc;
			glow.blurX=glow.blurY=6;
			bottom.filters=[glow];
			                 
			var up:Shape=new Shape();
			addChild(up);
			up.graphics.lineStyle();
			up.graphics.beginFill(0xFFFFFF);
			up.graphics.drawCircle(0,0,25);
			up.graphics.endFill();
			up.filters=[glow];
			
			bar_control=new Sprite();
			bar_control.buttonMode=true;
			bar_control.filters=[glow];
			bar_control.x=0;
			bar_control.y=-36;
			addChild(bar_control);
			bar_control.graphics.beginFill(0xFFFFFF);
			bar_control.graphics.drawCircle(0,0,10);
			bar_control.graphics.endFill();
			bar_control.addEventListener(MouseEvent.MOUSE_DOWN,onStaDragHandler);
			bar_control.stage.addEventListener(MouseEvent.MOUSE_UP,onStopDragHandler);


           //设置显示区域
			var format:TextFormat=new TextFormat ();
			format.size=10;
			msg=new TextField();
			//msg.embedFonts=true;
			msg.setTextFormat(format);
			msg.text=String(this.minValue);
			msg.width=25;
			msg.height=16;
			msg.x=-5;
			msg.y=-10;
			msg.selectable=false;
			addChild(msg);
		
		}

		private function onStaDragHandler(event:MouseEvent):void
		{
			bar_control.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler);			
		}
		private function onMoveHandler(event:MouseEvent):void
		{
			//编写控制的角度
			var dy:Number=mouseY;
			var dx:Number=mouseX;
			var angle:Number=Math.atan2(dy,dx);
			var tempX:Number=36*Math.cos(angle);
			var tempY:Number=36*Math.sin(angle);			
			bar_control.x=tempX;
			bar_control.y=tempY;		
			
			 trace(Math.round(angle*180/Math.PI));
			
			if(bar_control.y<=0 && bar_control.x<0)
			this.value=Math.round((angle*180/Math.PI+450)/360*maxValue+minValue);
			else
			this.value=Math.round((angle*180/Math.PI+90)/360*maxValue+minValue);
			 
			msg.text=String(this.value);
			
			var evt:Event=new Event(Event.CHANGE);
			this.dispatchEvent(evt);
		}
		private function onStopDragHandler(event:MouseEvent):void
		{
			bar_control.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler);
		}
		//设置拖动的值
		public function set value(dataValue:Number):void
		{
			this.values=dataValue;
		}
		//返回拖动的值
		public function get value():Number
		{
			return values;
		}
		//设置对象坐标
		public function move(x:Number,y:Number):void
		{
			this.x=x;
			this.y=y;
		}
	}

}